class Admin::ArticlesController < Admin::BasesController
  def index
    @title = t("h2.articles")
    @search = Article.new_search(params[:search])
    unless params[:keyword].blank?
      keyword = split_keyword(params[:keyword])
      @search.conditions.or_title_like = keyword
      @search.conditions.or_body_like = keyword
      @search.conditions.category.or_name_like = keyword
    end
    @articles = @search.all.paginate(:page => params[:page] || 1, :per_page=> params[:per_page] || 50)
  end

  def new
    @title = t('h2.new_article')
    @article = Article.new
  end

  def create
    @article = Article.new(params[:article])
    if @article.save
      flash[:notice] = "创建成功！"
      redirect_to admin_articles_path
    else
      render :action => :new
    end
  end

  def edit
    @title = t("h2.edit_article")
    @article = Article.find(params[:id])
  end

  def show
    @article = Article.find(params[:id])
    render :edit
  end

  def update
    @article = Article.find(params[:id])
    if @article.update_attributes(params[:article])
      flash[:notice] = "修改成功！"
      redirect_to admin_articles_path
    else
      render :action => 'edit'
    end
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    respond_to do |format|
      format.html { redirect_to(admin_articles_url) }
      format.xml { head :ok }
    end
  end
end
